package com.cindata

import static org.springframework.http.HttpStatus.*
import grails.transaction.Transactional
import org.springframework.security.access.annotation.Secured

@Secured(['isAuthenticated()'])
@Transactional(readOnly = true)
class BatchMortgageController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index() {
      def max = Math.min(params.max ? params.max.toInteger() : 10, 100)
      def offset = params.offset ? params.offset.toInteger() : 0
      def batchId = params["batchId"]
      def serialNumber = params["serialNumber"]
      def sql = " from BatchMortgage where 1=1 "
      def queryParam = [:]
      if (batchId)
      {
        sql += " and abnormalMortgageBatch.id =  :batchId "
        queryParam['batchId'] = Long.parseLong(batchId)
      }
      if (serialNumber && serialNumber != "")
      {
        sql += " and mortgage.serialNumber like :serialNumber "
        queryParam['serialNumber'] = "%" + serialNumber + "%"
      }
      sql += " order by modifiedDate desc"
      def batchMortgageList = BatchMortgage.executeQuery(sql, queryParam, [max: max, offset: offset])
      def batchMortgageCount = BatchMortgage.executeQuery(sql, queryParam)

      respond batchMortgageList, model:[batchMortgageCount: batchMortgageCount, params: params]
    }

    def show(BatchMortgage batchMortgage) {
        respond batchMortgage
    }

    def create() {
        respond new BatchMortgage(params)
    }

    @Transactional
    def save(BatchMortgage batchMortgage) {
        if (batchMortgage == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (batchMortgage.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond batchMortgage.errors, view:'create'
            return
        }

        batchMortgage.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'batchMortgage.label', default: 'BatchMortgage'), batchMortgage.id])
                redirect batchMortgage
            }
            '*' { respond batchMortgage, [status: CREATED] }
        }
    }

    def edit(BatchMortgage batchMortgage) {
        respond batchMortgage
    }

    @Transactional
    def update(BatchMortgage batchMortgage) {
        if (batchMortgage == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (batchMortgage.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond batchMortgage.errors, view:'edit'
            return
        }

        batchMortgage.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'batchMortgage.label', default: 'BatchMortgage'), batchMortgage.id])
                redirect batchMortgage
            }
            '*'{ respond batchMortgage, [status: OK] }
        }
    }

    @Transactional
    def delete(BatchMortgage batchMortgage) {

        if (batchMortgage == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        batchMortgage.delete flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'batchMortgage.label', default: 'BatchMortgage'), batchMortgage.id])
                redirect action:"index", method:"GET"
            }
            '*'{ render status: NO_CONTENT }
        }
    }

    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'batchMortgage.label', default: 'BatchMortgage'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }
}
